package com.yxp.easy.permissionutil.adapter;

import android.support.v4.view.MotionEventCompat;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.yxp.easy.permissionutil.Indo;
import com.yxp.easy.permissionutil.R;
import com.yxp.easy.permissionutil.adapter.drag.ItemTouchHelperAdapter;
import com.yxp.easy.permissionutil.adapter.drag.OnStartDragListener;

import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2016/4/19.
 */
public class HeSuanAdapter extends BaseRecyclerViewAdapter<Indo, HeSuanAdapter.HeSuanViewHolder>   implements ItemTouchHelperAdapter {

    private final OnStartDragListener mDragStartListener;
    public HeSuanAdapter(List<Indo> list,OnStartDragListener mDragStartListener) {
        super(list);
        this.mDragStartListener = mDragStartListener;
    }


    @Override
    protected void bindDataToItemView(HeSuanViewHolder myViewHolder, Indo item) {
        if(item.getType()>0){
            myViewHolder.setText(R.id.index_item_title,item.getIndex()+"-title");
        }

    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getType();

    }

    @Override
    public HeSuanViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
        Log.i("TAG","type="+type);
        //显示顶部信息
        final HeSuanViewHolder view  ;
        if (type == 0) {
              view =  new HeSuanViewHolder(inflateItemView(viewGroup, R.layout.he_suan_item_top));

            return view;
        } else  {
            view =   new HeSuanViewHolder(inflateItemView(viewGroup, R.layout.he_suan_item)) ;

            view.setOnTouchListener(R.id.index_item_iv,new View.OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                        mDragStartListener.onStartDrag(view);
                    }
                    return false;
                }
            });
            return view;
        }

    }


    public class HeSuanViewHolder extends BaseRecyclerViewAdapter.SparseArrayViewHolder {
        public HeSuanViewHolder(View itemView) {
            super(itemView);

        }


    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        Collections.swap(super.mList, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onItemDismiss(int position) {
        super.mList.remove(position);
        notifyItemRemoved(position);
    }
}
